package xiangmu.schoolpaper.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import xiangmu.schoolpaper.result.Result;
import xiangmu.schoolpaper.service.UploadUsersService;

@RestController
@RequestMapping("/uploadusers")
@Slf4j
@Tag(name = "用户信息文件上传接口")
public class UploadUsersController {
    @Autowired
    private UploadUsersService uploadUsersService;

    @Operation(summary = "上传教师信息")
    @PostMapping("/uploadteachers")
    public Result uploadTeachers(@RequestParam("file") MultipartFile file) {
        log.info("接收到教师信息上传请求: {}", file.getOriginalFilename());
        try {
            String response = uploadUsersService.uploadTeachersToFlask(file);
            return Result.success(response);
        } catch (Exception e) {
            log.error("上传失败: {}", e.getMessage(), e);
            return Result.error("文件上传失败");
        }
    }

    @Operation(summary = "上传学生信息")
    @PostMapping("/uploadstudents")
    public Result uploadStudents(@RequestParam("file") MultipartFile file) {
        log.info("接收到学生信息上传请求: {}", file.getOriginalFilename());
        try {
            String response = uploadUsersService.uploadStudentsToFlask(file);
            return Result.success(response);
        } catch (Exception e) {
            log.error("上传失败: {}", e.getMessage(), e);
            return Result.error("文件上传失败");
        }
    }

}
